package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.OrderItemQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.OrderItemDao;
import cn.org.eshow.model.OrderItem;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("orderItemDao")
public class OrderItemDaoHibernate extends GenericDaoHibernate<OrderItem, Integer>
        implements OrderItemDao {


    public OrderItemDaoHibernate() {
        super(OrderItem.class);
    }

    @Override
    public List<OrderItem> list(OrderItemQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<OrderItem> search(OrderItemQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(OrderItemQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(OrderItem.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, OrderItemQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (StringUtils.isNotEmpty(query.no)) {
                rule.add(Restrictions.like("no", query.no, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.type)) {
                rule.add(Restrictions.like("type", query.type, MatchMode.ANYWHERE));
            }
            if (query.price != null) {
                rule.add(Restrictions.eq("price", query.price));
            }
            if (query.minQuantity != null) {
                rule.add(Restrictions.ge("quantity", query.minQuantity));
            }
            if (query.maxQuantity != null) {
                rule.add(Restrictions.le("quantity", query.maxQuantity));
            }
            if (query.ifComment != null) {
                rule.add(Restrictions.eq("ifComment", query.ifComment));
            }
            if (query.productItemId != null) {
                rule.add(Restrictions.eq("productItem.id", query.productItemId));
            }
            if (query.productId != null) {
                rule.add(Restrictions.eq("product.id", query.productId));
            }
            if (query.productOrderId != null) {
                rule.add(Restrictions.eq("productOrder.id", query.productOrderId));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
